package cn.fl.mcr.service.impl;

import java.util.ArrayList;
import java.util.List;

import cmm.user.facade.system.api.ISysUsrQueryApiService;
import cmm.user.facade.system.condition.SysUsrSysRQueryCondition;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.mcr.service.IMcrCrTypCloseService;
import cn.fl.mcr.transform.McrCrTypCloseVOTransform;
import cn.fl.mcr.transform.McrPrpsRateVOTransform;
import cn.fl.mcr.vo.McrCityOpenVO;
import cn.fl.mcr.vo.McrCrTypCloseVO;
import cn.fl.mcr.vo.McrPrpsRateVO;
import cn.fl.system.transform.SysUsrSysRVOTransform;
import cn.fl.system.vo.SysUsrSysRVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.condition.McrCityOpenCtrlQC;
import cn.lionbridgecapital.loan.model.dto.McrCityOpenCtrlDTO;
import cn.lionbridgecapital.loan.model.dto.McrCityOpenDTO;
import cn.lionbridgecapital.loan.model.dto.McrCrTypCloseDTO;
import cn.lionbridgecapital.loan.model.dto.McrPrpsRateDTO;
import cn.lionbridgecapital.loan.service.api.IMcrCityOpenCtrlApiService;
import cn.lionbridgecapital.loan.service.api.IMcrCrTypCloseApiService;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description:  客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class McrCrTypCloseServiceImpl implements IMcrCrTypCloseService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IMcrCrTypCloseService.class);

	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IMcrCrTypCloseApiService mcrCrTypCloseApiService;
	@Autowired
	private IMcrCityOpenCtrlApiService mcrCityOpenCtrlApiService;

	@Autowired
	private ISysUsrQueryApiService sysUsrQueryApiService;

	@Override
	public McrCrTypCloseVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		return McrCrTypCloseVOTransform.toVO(this.mcrCrTypCloseApiService.selectMcrCrTypCloseDTOById(id));
	}

	@Override
	public Long add(McrCrTypCloseVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.mcrCrTypCloseApiService.addMcrCrTypClose(McrCrTypCloseVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.mcrCrTypCloseApiService.removeById(id);
	}

	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
	}

	@Override
	public boolean modify(McrCrTypCloseVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.mcrCrTypCloseApiService.modifyMcrCrTypClose(McrCrTypCloseVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.mcrCrTypCloseApiService.searchMcrCrTypCloseListPage(page);
		if(null != page && CollectionUtils.isNotEmpty(page.getResultList())){
			List<McrCrTypCloseVO> voList = McrCrTypCloseVOTransform.toVOList((List<McrCrTypCloseDTO>)page.getResultList());
			for (McrCrTypCloseVO vo : voList) {
				if(StringUtils.isNotEmpty(vo.getCrtUsrId())){
					//获取创建人用户信息
					SysUsrSysRQueryCondition sysRQueryConditionCrt = new SysUsrSysRQueryCondition();
					sysRQueryConditionCrt.setUsrId(vo.getCrtUsrId());
					sysRQueryConditionCrt.setSysCd(PropertiesCacheUtils.getProperty(SystemConstant.FL_SERVICE_CD));
					sysRQueryConditionCrt.setIsDel(BaseConstant.IS_YESNO_NO);
					List<SysUsrSysRVO> usrList = SysUsrSysRVOTransform.toVOList(sysUsrQueryApiService.searchSysUsrSysRList(sysRQueryConditionCrt));
					if(CollectionUtils.isNotEmpty(usrList)){
						vo.setCrtNm(usrList.get(0).getRlNm());
					}
				}

				if(StringUtils.isNotEmpty(vo.getMdfUsrId())){
					//获取更信任用户信息
					SysUsrSysRQueryCondition sysRQueryConditionMdf = new SysUsrSysRQueryCondition();
					sysRQueryConditionMdf.setUsrId(vo.getMdfUsrId());
					sysRQueryConditionMdf.setSysCd(PropertiesCacheUtils.getProperty(SystemConstant.FL_SERVICE_CD));
					sysRQueryConditionMdf.setIsDel(BaseConstant.IS_YESNO_NO);
					List<SysUsrSysRVO> mdfList = SysUsrSysRVOTransform.toVOList(sysUsrQueryApiService.searchSysUsrSysRList(sysRQueryConditionMdf));
					if(CollectionUtils.isNotEmpty(mdfList)){
						vo.setMdfNm(mdfList.get(0).getRlNm());
					}
				}

			}
			page.setResultList(voList);
		}
		return page;
	}
}
